<template>
	<view class="wrap">
		<js-error mode="bottom" ref="jsError"></js-error>
		<view class="search cu-bar">
			<u-search  v-model="barCode" :focus="focus" @confirm="search" @custom="search" @search="search" placeholder="扫一扫"  :show-action="false"></u-search>
			<view class="action" @click="toScan">
				<text class="cuIcon-scan" style="font-size: 56rpx;"></text>
			</view>
		</view>
		
		<scroll-view class="scroll-list bg-white" scroll-y="true" :style="[{height:stockListHeight + 'px'}]">
			<u-form class="form bg-white" :model="model" ref="uForm" label-position="left">
				<u-form-item label="存货编码:" prop="cinvCode" label-width="180" :label-style="{'font-weight':'bold'}">
					<view>{{model.basInv.cinvcode}}</view>
				</u-form-item>
				<u-form-item label="存货代码:" prop="cinvName" label-width="180" :label-style="{'font-weight':'bold'}">
					<view>{{model.basInv.cinvaddcode}}</view>
				</u-form-item>
				<u-form-item label="存货名称:" prop="cinvName" label-width="180" :label-style="{'font-weight':'bold'}">
					<view>{{model.basInv.cinvname}}</view>
				</u-form-item>
				<u-form-item label="仓库:" prop="cwhCode" label-width="180" :label-style="{'font-weight':'bold'}">
					<view>{{model.cwhcode}}</view>
				</u-form-item>
				<u-form-item label="批次:" prop="pcno" label-width="180" :label-style="{'font-weight':'bold'}">
					<view>{{model.pcno}}</view>
				</u-form-item>
				<u-form-item label="数量:" prop="qty" label-width="180" :label-style="{'font-weight':'bold'}">
					<u-input placeholder="请输入入库数量" v-model="model.qty" required type="number"></u-input>
				</u-form-item>
				<u-form-item label="货位:" prop="cbatch" label-width="180"  :label-style="{'font-weight':'bold'}">
					<u-input placeholder="请选择货位" v-model="cposCodeAndName" @click="region" disabled="disabled" type="select" di
						maxlength="64"></u-input>
				</u-form-item>
			</u-form>
		</scroll-view>
		
		<view class="footer">
			<view class="confrim-btn padding padding-bottom-sm flex bg-white">
				<u-button  type="primary" class="btn" style="width: 100%;" @click="submit">提交</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				focus: true,
				stockListHeight: 0,
				barCode:'', //AA01|01GD01600500I010|ST12 1.6*140*C|Z1110620220905(2)220907112841001|004|949|1|202209071128|0AAA||20220905(2)||9103277|11106||||||
				showflag:0,
				model: {
					basInv:{},
				},
				cposCodeAndName:'',
			}
		},
		onLoad(params) {
			var _self = this;
			uni.getSystemInfo({
				success: (e) => { // resu 可以获取当前屏幕的高度
					_self.stockListHeight = e.windowHeight - uni.upx2px(160);
				},
				fail: (res) => {}
			});
			
			uni.$on('consult', (item) => {
				this.cposCodeAndName = item.cposcode + ' - ' + item.cposname;
				this.model.cposcode = item.cposcode;
			})
		},
		watch: {
		
		},
		onShow() {},
		onReady() {},
		methods: {
			region(){
				if(this.model.cwhcode != undefined && this.model.cwhcode != ''){
					uni.navigateTo({
						url: '/pages/asd/index/cgsj/region?cwhcode='+this.model.cwhcode
					})
				}else{
					_this.$refs.jsError.showError('','请扫描现品票！','error');
				}
			},
			toScan(){
				var _this = this;
				uni.scanCode({ 
					onlyFromCamera: true,
					success: function(res) {
						this.barCode = res.result;
						this.search();
					}.bind(this)
				});
			},
			search(){
				this.loadData(this.barCode);
			},
			
			loadData(barcode){
				this.$u.api.asdApi.getXppData({barCode:this.barCode}).then(res => {
					this.barCode = '';
					this.model = res.data;
				})
			},
			
			async submit() {
				var i = await this.checkData();
				if(i == 'suc'){
					this.$u.api.qt.saveUpData(this.model).then(res => {
						uni.showModal({
							title: '提示',
							content: res.message,
							showCancel: false,
							success: (res) => {}
						});
						if(res.result == 'true'){
							uni.navigateBack({
								delta: 1,
							})
						}
					})
				}
			},
			
			checkData(){
				const _this = this;
				return new Promise((resolve,reject) =>{
					if(_this.model.cposcode == undefined || _this.model.cposcode == ''){
						_this.$refs.jsError.showError('','请选择货位！','error');
						resolve("err");
					}
					resolve("suc");
				})
			},
		}
	}
</script>
<style>
	@import '../asd.css';
</style>
